package servlet;

import pojo.AccessControlDevice;
import service.AccessControlDeviceService;
import service.impl.AccessControlDeviceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



@WebServlet(urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求的字符编码
        request.setCharacterEncoding("UTF-8");

        //设置响应的字符编码
        response.setCharacterEncoding("UTF-8");

        //设置内容
        response.setContentType("text/html");

        //获取数据
        String principal = request.getParameter("principal");
        String supplier = request.getParameter("supplier");
        String brand = request.getParameter("brand");
        Integer buildingId = Integer.parseInt(request.getParameter("buildingId"));
        String addTime = request.getParameter("addTime");


        AccessControlDevice a = new AccessControlDevice();
        a.setPrincipal(principal);
        a.setSupplier(supplier);
        a.setBrand(brand);
        a.setBuildingId(buildingId);
        a.setAddTime(addTime);


        AccessControlDeviceService accessControlDeviceService =new AccessControlDeviceServiceImpl();

        int add = accessControlDeviceService.add(a);

        PrintWriter out = response.getWriter();
        if (add>0){
            out.print("<script>alert(\"添加成功！\");location.href='list';</script>");
        }else{
            out.print("<script>alert(\"添加失败！\");location.href='add.jsp';</script>");
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
